package com.sophialex.health.record.views;

import java.util.List;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.part.ViewPart;

import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.person.vo.HrPerson;
import com.sophialex.health.record.Messages;
import com.sophialex.health.record.action.CancelFamilyRelationAction;
import com.sophialex.health.record.action.OpenRecordAction;
import com.sophialex.health.record.common.util.DataSource;
import com.sophialex.health.record.common.util.EventType;
import com.sophialex.health.record.common.util.HealthRecordListenerRegister;
import com.sophialex.health.system.lktable.LKCellStyleProvider;
import com.sophialex.health.system.lktable.LKIGetJoinOutValue;
import com.sophialex.health.system.lktable.LKTable;
import com.sophialex.health.system.lktable.SWTX;
import com.sophialex.health.system.lktable.LKTableUtil.ECellEditType;

public class PersonTopRightView extends ViewPart implements IPropertyChangeListener{

	public static final String ID = "com.sophialex.health.record.views.PersonTopRightView"; //$NON-NLS-1$
	public LKTable lktable;
	List<HrPerson> hrPersonList;
	public CancelFamilyRelationAction cancelFalilyRelationAction;
	public OpenRecordAction operRecordAction;
	public PersonTopRightView() {
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		lktable = new LKTable(parent, SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY
			      				| SWTX.WRAP | SWT.VERTICAL | SWT.HORIZONTAL);

		fillTable()	;
		createActions();
		initializeToolBar();
		initializeMenu();
		HealthRecordListenerRegister.getInstance().addPropertyChangeListener(this);
	}
    
	public void fillTable() {
		
		lktable.doAddColumn(0, Messages.getString("Family.Member.0"), "name", 50, 
		    				SWTX.ALIGN_HORIZONTAL_CENTER, ECellEditType.textBox, false,
		    				false);
		lktable.doAddColumn(1, Messages.getString("Family.Member.1"), "sex", 50,
		    				SWTX.ALIGN_HORIZONTAL_CENTER, ECellEditType.textBox, false,
		    				false);
		lktable.doAddColumn(2, Messages.getString("Family.Member.2"), "perRelaMaster", 50,
		    				SWTX.ALIGN_HORIZONTAL_CENTER, ECellEditType.textBox, false,
		    				false);
		lktable.setHeaderHeight(30);
		lktable.exeLKTable(HrPerson.class);
		lktable.setJoinOut(new LKIGetJoinOutValue() {
			public String getFieldValue(Object obj, String fieldName, Object byValue) {
				if ("sex".equals(fieldName)) {
					if(byValue!=null){
						return SuperContext.dictCache.getCodeValueByNum("SEX",byValue.toString());
					}
				}
				if ("perRelaMaster".equals(fieldName)) {
					if(byValue!=null){
						return SuperContext.dictCache.getCodeValueByNum("RELATION",byValue.toString());
					}
				}
				return byValue == null ? "" : byValue.toString(); 
			}
		});
		lktable.setCellStyleProvider(new LKCellStyleProvider(){
			public String getTipInfo(Object element,String field,Object byValue){
				if(byValue!=null){
					if ("sex".equals(field)) {
						if(byValue!=null){
							return SuperContext.dictCache.getCodeValueByNum("SEX",byValue.toString());
						}
					}
					if ("perRelaMaster".equals(field)) {
						if(byValue!=null){
							return SuperContext.dictCache.getCodeValueByNum("RELATION",byValue.toString());
						}
					}
					return byValue.toString();
				}else{
					return null;
				}
			}
		});
		lktable.setCanTraverse(false);
		setData();
	}
	public void setData() {
		try {
			HrPerson currentPerson = SuperContext.currentPerson;
			if(currentPerson != null && currentPerson.getPerFamilyId() != null){
				hrPersonList = DataSource.hrPersonService.findFamilyPerson(currentPerson.getPerFamilyId());
				lktable.setInput(hrPersonList);
				cancelFalilyRelationAction.setHrPersonList(hrPersonList);
			}else{
				lktable.setInput(null);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void dispose() {
		super.dispose();
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		
		cancelFalilyRelationAction = new CancelFamilyRelationAction(getSite().getWorkbenchWindow(), lktable, hrPersonList);
		operRecordAction = new OpenRecordAction(getSite().getWorkbenchWindow(),lktable);
	};

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
		tbm.add(cancelFalilyRelationAction);	
		tbm.add(operRecordAction);
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuMgr = getViewSite().getActionBars().getMenuManager();
		MenuManager menuM = new MenuManager();
		Menu menu = menuM.createContextMenu(lktable);
		menuM.add(cancelFalilyRelationAction);
		menuM.add(operRecordAction);
		lktable.setMenu(menu);
		
	}

	@Override
	public void setFocus() {
		
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if(event.getProperty().equals(EventType.REFRESH)){
			setData();
		}
		
	}

}
